package com.ibiaoke.order.dao;


import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.customer.dao.PtOpenGroupDao;
import com.ibiaoke.customer.entity.PtFightGroup;
import com.ibiaoke.customer.entity.PtOpenGroup;
import com.ibiaoke.customer.service.FightGroupService;
import com.ibiaoke.customer.service.OpenGroupService;
import com.ibiaoke.order.consts.PtOrderStatusConst;
import com.ibiaoke.order.entity.Order;
import com.ibiaoke.order.entity.PtOrder;
import com.ibiaoke.order.entity.PtOrderExample;
import com.ibiaoke.order.entity.so.OrderSO;
import com.ibiaoke.order.entity.so.PtOrderSO;
import com.ibiaoke.order.mapper.PtOrderMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.Currency;
import java.util.Date;
import java.util.List;

@Repository
public class PtOrderDao extends BaseDao<PtOrder, PtOrderExample, Integer, PtOrderMapper>{

    @Autowired
    private PtOrderMapper ptOrderMapper;

    @Autowired
    private PtOpenGroupDao ptOpenGroupDao;

    @Autowired
    private OpenGroupService openGroupService;

    @Autowired
    private FightGroupService fightGroupService;

    @Override
    public PtOrderMapper getMapper() {
        return ptOrderMapper;
    }

    public List<PtOrder>  GetPtOrder(PtOrder ptOrder,Page page,String isEcxel){

        PtOrderExample example = new PtOrderExample();
        PtOrderExample.Criteria criteria = example.createCriteria();
        if (ptOrder.getPtOrderCode() != null && !"".equals(ptOrder.getPtOrderCode())) {
            criteria.andPtOrderCodeEqualTo(ptOrder.getPtOrderCode());
        }
        /*if(ptOrder.getContactMobile() != null && !"".equals(ptOrder.getContactMobile())){
            criteria.andContactMobileEqualTo(ptOrder.getContactMobile());
        }*/
        if(ptOrder.getCreateTime() != null){
            criteria.andUpdateTimeGreaterThan(ptOrder.getCreateTime());
        }
        if(ptOrder.getEndTime() != null){
            criteria.andUpdateTimeLessThan(ptOrder.getEndTime());
        }
        if(ptOrder.getPtStatus() != null){
            criteria.andPtStatusEqualTo(ptOrder.getPtStatus());
        }
        if(ptOrder.getDeliveryType() != null){
            criteria.andDeliveryTypeEqualTo(ptOrder.getDeliveryType());
        }
        if(ptOrder.getGoodId() != null){
            criteria.andGoodIdEqualTo(ptOrder.getGoodId());
        }
        if(ptOrder.getUserId() != null){
            criteria.andUserIdEqualTo(ptOrder.getUserId());
        }
        if (page != null && isEcxel.equals("0")) {
            page.setTotalCount(ptOrderMapper.countByExample(example));
            example.setLimitStart(page.limitStart());
            example.setLimitEnd(page.limitEnd());
        }
        example.setOrderByClause("update_time desc ");

        return  ptOrderMapper.selectByExample(example);

    }


    public List<PtOrder>  getOrderDeatil(String ptOrderCode){
        PtOrderExample example = new PtOrderExample();
        PtOrderExample.Criteria criteria = example.createCriteria();
        if (ptOrderCode != null && !"".equals(ptOrderCode)) {
            criteria.andPtOrderCodeEqualTo(ptOrderCode);
        }
        return  ptOrderMapper.selectByExample(example);
    }


    public Integer savePtOrder(PtOrder ptOrder){
        return ptOrderMapper.insert(ptOrder);
    }


    public List<PtOrder> getMyPtOrder(Integer userId,Page page){
        PtOrderExample example = new PtOrderExample();
        PtOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andIsdeleteEqualTo(0);
        if (page != null) {
            page.setTotalCount(ptOrderMapper.countByExample(example));
            example.setLimitStart(page.limitStart());
            example.setLimitEnd(page.limitEnd());
        }
        example.setOrderByClause("create_time desc");
        return ptOrderMapper.selectByExample(example);
    }


    public Integer upPtOrderStatus(Integer orderId, Integer status) {
    PtOrder ptOrder = new PtOrder();
    ptOrder.setPtOrderId(orderId);
    ptOrder.setPtStatus(status);
    return updateByPrimaryKeySelective(ptOrder);
    }

    public Integer updatePtOrderIsDeteleStatus(Integer orderId){
        PtOrder ptOrder = new PtOrder();
        ptOrder.setPtOrderId(orderId);
        ptOrder.setIsdelete(1);
        return updateByPrimaryKeySelective(ptOrder);
    }

    public PtOrder GetPtOrderById(Integer orderId){
        return selectByPrimaryKey(orderId);
    }

    public Integer upPtOrderTransaction(Integer orderId, String TransactionNo) {
        PtOrder ptOrder = new PtOrder();
        ptOrder.setPtOrderId(orderId);
        ptOrder.setTransactionNo(TransactionNo);
        return updateByPrimaryKeySelective(ptOrder);
    }

    public Integer upPtOrder(PtOrder ptOrder) {
        return updateByPrimaryKeySelective(ptOrder);
    }

    //获取所有订单状态为status的订单
    public List<PtOrder> getPtOrderByStatus(Integer status){
        PtOrderExample example = new PtOrderExample();
        PtOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPtStatusEqualTo(status);
        List<PtOrder>  ptOrderList = ptOrderMapper.selectByExample(example);
        return ptOrderList;
    }

    //更改订单创建时间大于10天的订单状态为5(已收货)
    public  void updateConfrimPtOrder(List<PtOrder>  ptOrderList){
        for(PtOrder ptOrder : ptOrderList){
            Date nowDate = new Date();
            Date createDate = ptOrder.getCreateTime();
            Long time = nowDate.getTime()-createDate.getTime();
            Long time2 = new Long((long)240*60*60*1000);
            if(time>time2){
                upPtOrderStatus(ptOrder.getPtOrderId(),5);
            }
        }
    }


    //首先，判断是否为开团订单，查询出开团的创建时间
    //超过二十四小时，并且状态为4(待成团)，更新开团的状态为3(未成团)
    public void updateOverTimePtOrder(List<PtOrder>  ptOrderList){
        for(PtOrder ptOrder : ptOrderList){
            if(ptOrder.getIsopen()==1){
                Date nowDate = new Date();
                PtOpenGroup ptOpenGroup = ptOpenGroupDao.getPtOpenGroupByKid(ptOrder.getKid());
                    Long time = nowDate.getTime()-ptOpenGroup.getCreateTime().getTime();
                    Long time2 = new Long((long)24*60*60*1000);
                    if(time>time2){
                       List<PtOrder>  listPtOrder =  getPtOrderByOpenCode(ptOpenGroup.getOpenCode());
                        for(PtOrder PtOrders :listPtOrder){
                            PtOpenGroup ptOpenGroup1 = ptOpenGroupDao.getPtOpenGroupByKid(PtOrders.getKid());
                            if(ptOpenGroup1.getIsPay() == 1){
                                upPtOrderStatus(PtOrders.getPtOrderId(),3);
                            }
                        }
                    }
            }
        }
    }

    //通过kid，查询PtOrder
    public List<PtOrder> getPtOrderByKid(Integer kid){
        PtOrderExample example = new PtOrderExample();
        PtOrderExample.Criteria criteria = example.createCriteria();
        criteria.andKidEqualTo(kid);
        List<PtOrder>  ptOrderList = ptOrderMapper.selectByExample(example);
        return ptOrderList;
    }

    public List<PtOrder> getPtOrderByCode(String ordercCode){
        PtOrderExample example = new PtOrderExample();
        PtOrderExample.Criteria criteria = example.createCriteria();
        criteria.andPtOrderCodeEqualTo(ordercCode);
        List<PtOrder>  ptOrderList = ptOrderMapper.selectByExample(example);
        return ptOrderList;

    }

    public void updateWaitPayPtOrder(){
        //查询状态为2（待付款）的订单
        List<PtOrder> ptOrderList =getPtOrderByStatus(2);
        //订单创建1小时之后，更改状态为7（已取消）
        for(PtOrder ptOrder : ptOrderList){
            if(System.currentTimeMillis()-ptOrder.getCreateTime().getTime()>60*60*1000){
                upPtOrderStatus(ptOrder.getPtOrderId(),7);
            }
        }
    }

    //修改delete状态
    public Integer updateDelete(Integer orderId,Integer delete){
        PtOrder ptOrder = new PtOrder();
        ptOrder.setPtOrderId(orderId);
        ptOrder.setIsdelete(delete);
        return updateByPrimaryKeySelective(ptOrder);
    }

    //通过opencode获取order集合
    public List<PtOrder> getPtOrderByOpenCode(String openCode){
        PtOrderExample example = new PtOrderExample();
        PtOrderExample.Criteria criteria = example.createCriteria();
        criteria.andOpenCodeEqualTo(openCode);
        return ptOrderMapper.selectByExample(example);
    }

    //拼团成功，修改更新时间为成团时间
    public Integer updateUpdateTime(Integer id){
        PtOrder ptOrder = new PtOrder();
        ptOrder.setPtOrderId(id);
        ptOrder.setUpdateTime(new Date());
        return updateByPrimaryKeySelective(ptOrder);
    }

    public List<PtOrder> listOrder(PtOrderSO ptOrderSO, Page page) {
        PtOrderExample ptOrderExample = new PtOrderExample();
        PtOrderExample.Criteria criteria = ptOrderExample.createCriteria();
        if (StringUtils.isNotBlank(ptOrderSO.getPtOrderCode())) {
            criteria.andPtOrderCodeLike("%" + ptOrderSO.getPtOrderCode() + "%");
        }

        if (ptOrderSO.getBeginTime() != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(ptOrderSO.getBeginTime());
        }
        if (ptOrderSO.getEndTime() != null) {
            criteria.andCreateTimeLessThan(ptOrderSO.getEndTime());
        }

        if (ptOrderSO.getPtStatus() != null) {
            //待收货
            if (ptOrderSO.getPtStatus() == -1) {
                criteria.andPtStatusIn(Arrays.asList(PtOrderStatusConst.RECEIVED));
            }else {
                criteria.andPtStatusEqualTo(ptOrderSO.getPtStatus());
            }
        }
        /*if (ptOrderSO.getShopId() != null) {
            criteria.andShopIdEqualTo(ptOrderSO.getShopId());
        }*/

        if (ptOrderSO.getSource() != null) {
            criteria.andSourceEqualTo(ptOrderSO.getSource());
        }

        if (ptOrderSO.isDateAnalysis() == true) {
            criteria.andPtStatusIn(Arrays.asList(PtOrderStatusConst.RECEIVED));
        }

        if (page != null) {
            page.setTotalCount(ptOrderMapper.countByExample(ptOrderExample));
            ptOrderExample.setLimitStart(page.limitStart());
            ptOrderExample.setLimitEnd(page.limitEnd());
        }
        ptOrderExample.setOrderByClause("create_time desc ");
        return ptOrderMapper.selectByExample(ptOrderExample);
    }

    public List<PtOrder> aggregate(Integer serviceNum, Date startTime, Date endTime) {
        PtOrderExample example = new PtOrderExample();
        example.createCriteria().andServiceNumEqualTo(serviceNum).andCreateTimeGreaterThanOrEqualTo(startTime).andCreateTimeLessThan(endTime);
        return ptOrderMapper.selectByExample(example);
    }
}

